clear all
clear matrix
version 13
capture log close
set matsize 11000
set more off, permanently

*-------------------------------------------------------------------------------
* Options	
*-------------------------------------------------------------------------------
global stub 	 = "/Volumes/Transcend/AWFP/ThirdChapter_revision"
global main		 = "$stub/data"
global inputcali = "$stub/data/CalibrationInput"	
global outputcali= "$stub/data/CalibrationOutput"	
global data_proc = "$stub/data"
global graphs    = "$stub/graphs"	
global tables    = "$stub/tables"
global do        = "$stub/do"
global excel     = "$stub/excel"	
global orig      = "$stub/orig"

cd "$main"
********************************************************************************
import delimited ${outputcali}/toexport.csv, clear 

ren v1 amr
drop if amr==51

sum v3
local min2 = round(`r(min)')
sum v3
local max2 = round(`r(max)')
sum v4
local min3 = round(`r(min)')
sum v4
local max3 = round(`r(max)')

if `min2'<`min3'{
local min = `min2'
}
else{
local min = `min3'
}
if `max2'>`max3'{
local max = `max2'
}
else{
local max = `max3'
}

sum

ren v3 delta_model
ren v4 delta_data

sum
local N = `r(N)'
gen degree45 = `min'+amr*(`max'-`min')/`N'
twoway (scatter delta_model delta_data, msymb(o) msize(medium) mcolor(black)) (line degree45 degree45, lcolor(black)), xlabel(`min'(1)`max') ylabel(`min'(1)`max') legend(off) xtitle("Shape parameter: data") ytitle("Shape parameter: model") graphregion(color(white))
graph export ${graphs}/deltacorrelation.png, replace

ren v5 rho_model
ren v6 rho_data
drop degree45

sum v2
local min2 = round(`r(min)',.01)
sum v2
local max2 = round(`r(max)',.01)
sum rho_model
local min3 = round(`r(min)',.01)
sum rho_model
local max3 = round(`r(max)',.01)

if `min2'<`min3'{
local min = `min2'
}
else{
local min = `min3'
}
if `max2'>`max3'{
local max = `max2'
}
else{
local max = `max3'
}

sum
local N = `r(N)'
merge 1:1 amr using steadystate5.dta
drop if _merge!=3
drop _merge

merge 1:1 amr using citydata_721.dta
drop if _merge!=3
drop _merge
gen logpop = ln(m_empl)
drop v2
ren v9 S
ren v7 shrink
ren v8 expand
gen staysame = 1-shrink-expand

twoway (scatter shrink logpop, msymb(o) msize(medium) mcolor(black)) (lfit shrink logpop, lcolor(black)), graphregion(color(white)) xtitle("Log of average size") ytitle("Probability of shrinking") legend(off)
graph export ${graphs}/probshrink.png, replace
twoway (scatter expand logpop, msymb(o) msize(medium) mcolor(black)) (lfit expand logpop, lcolor(black)), graphregion(color(white)) xtitle("Log of average size") ytitle("Probability of growing") legend(off)
graph export ${graphs}/probexpand.png, replace
twoway (scatter staysame logpop, msymb(o) msize(medium) mcolor(black)) (lfit staysame logpop, lcolor(black)), graphregion(color(white)) xtitle("Log of average size") ytitle("Probability of staying the same") legend(off)
graph export ${graphs}/probstaysame.png, replace
twoway (scatter delta_model logpop, msymb(o) msize(medium) mcolor(black)) (lfit delta_model logpop, lcolor(black)), graphregion(color(white)) xtitle("Log of average size") ytitle("Shape parameter: model") legend(off)
graph export ${graphs}/deltamodel.png, replace

replace shrink = shrink*100
replace expand = expand*100
replace staysame = staysame*100
reg shrink logpop
reg expand logpop
reg staysame logpop
reg delta_model logpop

twoway (scatter S logpop, msymb(o) msize(medium) mcolor(black)) (lfit S logpop, lcolor(black)), graphregion(color(white)) xtitle("Log of average size") ytitle("S") legend(off)
graph export ${graphs}/nsteps.png, replace

save calibratedparameter.dta, replace
tabstat S shrink staysame expand, stat(p10 p50 p90)
********************************************************************************
* VOLATILITY COMPARISON PART: prepare data for export to matlab main_partII.m **
********************************************************************************
local year = 2020 	
global date = "2306"

import delimited using "${outputcali}/solution.csv", clear
sum v1
global sol = r(mean) /* optimal combination of parameter */

import delimited using ${inputcali}/count_avg_estimation.csv, clear
gen aux = 1
reshape long v, i(aux) j(amr)
drop aux
ren v firmcounts
save ${inputcali}/count_avg_estimation.dta, replace

import delimited using "${outputcali}/selectcities.csv", clear
gen aux = 1
reshape long v, i(aux) j(amr)
keep if v==7
sum
keep amr 
gen newamr=_n
save "${outputcali}/selectcities.dta", replace
********************************************************************************
use "${outputcali}/${date}`year'/tosend_${date}`year'_${sol}.dta", clear
drop aux*
egen id = group(amr eventdate)
reshape long v tot_empl, i(id) j(bin)
merge m:1 amr using "${outputcali}/selectcities.dta"
drop if _merge!=3
drop _merge
drop if v==0			/* employment value is equal to 0 */
drop amr
ren newamr amr
sum amr 
global N1 = `r(max)'
forv n = 1(1)$N1 {
preserve
qui: keep if amr==`n'
qui: ren tot_empl mu
qui: keep eventdate mu bin
qui: reshape wide mu, i(eventdate) j(bin)
drop eventdate
qui: export delimited using "$inputcali/shares`n'.csv", replace novarnames
restore
}
********************************************************************************
